package com.cq.hd.system.service.impl;

import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.system.po.TbContentPo;
import com.cq.hd.system.mapper.TbContentMapper;
import com.cq.hd.system.service.TbContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.hd.system.api.dto.ContentPageDto;
import com.cq.hd.system.api.dto.ContentSaveDto;
import com.cq.hd.system.api.dto.ContentUpdateDto;
import com.cq.hd.system.api.dto.ContentUpdateStateDto;
import com.cq.hd.system.api.vo.ContentPageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 学堂详情表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-19
 */
@Service
public class TbContentServiceImpl extends ServiceImpl<TbContentMapper, TbContentPo> implements TbContentService {

    @Override
    public Page<ContentPageVo> pageContent(ContentPageDto contentPageDto) {
        String startTime = contentPageDto.getStartTime();
        String endTime = contentPageDto.getEndTime();
        if (!StringUtils.isBlank(startTime) && startTime.length() != 19) {
            Throw.isBusinessException("日期格式错误");
        }
        if (!StringUtils.isBlank(endTime) && endTime.length() != 19) {
            Throw.isBusinessException("日期格式错误");
        }

        PageHelper.startPage(contentPageDto.getPageNum(), contentPageDto.getPageSize());
        List<ContentPageVo> contentPageVos = baseMapper.listContentByDto(contentPageDto);
        PageInfo<ContentPageVo> pageInfo = new PageInfo<>(contentPageVos);
        return Page.newPageWithAllFields(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages());
    }

    @Override
    public Long saveContent(ContentSaveDto contentSaveDto) {
        Integer contentSort = contentSaveDto.getContentSort();
        if(contentSort > 1000000){
            Throw.isBusinessException("排序不能超过1000000");
        }

        TbContentPo tbContentPo = new TbContentPo();
        BeanUtils.copyProperties(contentSaveDto, tbContentPo);
        tbContentPo.setCreateTime(LocalDateTime.now());
        int insert = baseMapper.insert(tbContentPo);
        if (insert == 0) {
            Throw.isBusinessException("保存失败");
        }
        return tbContentPo.getId();
    }

    @Override
    public Boolean updateContent(ContentUpdateDto contentUpdateDto) {
        Integer contentSort = contentUpdateDto.getContentSort();
        if(contentSort > 1000000){
            Throw.isBusinessException("排序不能超过1000000");
        }

        TbContentPo tbContentPo = baseMapper.selectById(contentUpdateDto.getId());
        if (tbContentPo == null) {
            Throw.isBusinessException("未找到文章数据");
        }

        BeanUtils.copyProperties(contentUpdateDto, tbContentPo);
        tbContentPo.setUpdateTime(LocalDateTime.now());
        int update = baseMapper.updateById(tbContentPo);
        if (update == 0) {
            Throw.isBusinessException("更新失败");
        }

        return true;
    }

    @Override
    public Boolean deleteContent(Long id) {
        TbContentPo tbContentPo = baseMapper.selectById(id);
        if (tbContentPo == null) {
            Throw.isBusinessException("未找到文章数据");
        }

        tbContentPo.setDelState(1);
        tbContentPo.setUpdateTime(LocalDateTime.now());
        int update = baseMapper.updateById(tbContentPo);
        if (update == 0) {
            Throw.isBusinessException("删除失败");
        }

        return true;
    }

    @Override
    public Boolean updateState(ContentUpdateStateDto contentUpdateStateDto) {
        // 状态（1-上架，2-下架）
        Integer state = contentUpdateStateDto.getState();
        if (state == null || (state != 1 && state != 2)) {
            Throw.isBusinessException("状态值错误");
        }

        TbContentPo tbContentPo = baseMapper.selectById(contentUpdateStateDto.getId());
        if (tbContentPo == null) {
            Throw.isBusinessException("未找到文章数据");
        }

        tbContentPo.setState(contentUpdateStateDto.getState());
        tbContentPo.setUpdateTime(LocalDateTime.now());
        int update = baseMapper.updateById(tbContentPo);
        if (update == 0) {
            Throw.isBusinessException("处理失败");
        }

        return true;
    }
}
